package com.example.tys.baseapplication.View;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.drawable.RotateDrawable;
import android.graphics.drawable.StateListDrawable;
import android.support.annotation.Nullable;
import android.support.v7.widget.SwitchCompat;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;
import android.widget.CompoundButton;

import com.example.tys.baseapplication.R;


/**
 * 帶状态的选择按钮。
 * 当状态改变时，新状态的动画{@link RotateDrawable}将会被开启</p>
 * {@link SwitchStatusButton#cancelAnimate()}关闭动画效果
 * </>
 *
 * @author tys
 * @date 2019/2/4
 */

public class SwitchStatusButton extends SwitchCompat implements CompoundButton.OnCheckedChangeListener {
    private RotateDrawable mThumbRotateDrawable;
    private StateListDrawable mThumbNormalDrawable;
    private ValueAnimator mWaitingAnimator;
    private long THUMB_WAITING_DURATION = 1500;
    private OnCheckedChangeListener onCheckedChangeListener;

    public SwitchStatusButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mThumbRotateDrawable = (RotateDrawable) getResources().getDrawable(R.drawable.switch_anim_rotate_thum);
        mThumbNormalDrawable = (StateListDrawable) getResources().getDrawable(R.drawable.switch_nor_thum);
        setThumbDrawable(mThumbNormalDrawable);
        setTrackResource(R.drawable.switch_button_track);
        super.setOnCheckedChangeListener(this);
    }

    @Override
    public void setOnCheckedChangeListener(@Nullable CompoundButton.OnCheckedChangeListener listener) {
        throw new IllegalArgumentException("not support");
    }

    public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
        this.onCheckedChangeListener = onCheckedChangeListener;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (!(getThumbDrawable() instanceof RotateDrawable)) {
            mThumbRotateDrawable.setState(new int[]{isChecked ?
                    android.R.attr.state_checked : -android.R.attr.state_checked});
            setThumbDrawable(mThumbRotateDrawable);
            animateThumbWaiting();
        }
        if (onCheckedChangeListener != null) {
            onCheckedChangeListener.onCheckChange((SwitchStatusButton) buttonView, isChecked);
        }
    }

    public void cancelAnimate() {
        if (mWaitingAnimator != null && mWaitingAnimator.isRunning()) {
            mWaitingAnimator.cancel();
        }
        mThumbNormalDrawable.setState(new int[]{isChecked() ?
                android.R.attr.state_checked : -android.R.attr.state_checked});
        setThumbDrawable(mThumbNormalDrawable);
    }

    private void animateThumbWaiting() {
        if (mWaitingAnimator != null) {
            mWaitingAnimator.cancel();
        }
        this.mWaitingAnimator = ValueAnimator.ofInt(0, 10000);
        this.mWaitingAnimator.setDuration(THUMB_WAITING_DURATION);
        this.mWaitingAnimator.setInterpolator(new LinearInterpolator());
        this.mWaitingAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Integer value = (Integer) animation.getAnimatedValue();
                if (getThumbDrawable() == mThumbRotateDrawable) {
                    mThumbRotateDrawable.setLevel(value);
                } else {
                    mWaitingAnimator.cancel();
                }
            }
        });
        this.mWaitingAnimator.setRepeatMode(ValueAnimator.RESTART);
        this.mWaitingAnimator.setRepeatCount(ValueAnimator.INFINITE);
        this.mWaitingAnimator.start();
    }

    public interface OnCheckedChangeListener {
        void onCheckChange(SwitchStatusButton switchStatusButton, boolean isChecked);
    }

}
